package com.yamanyar.weebill.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.validator.NotNull;

/**
 * 
 * <code>User</code>s has <code>Role</code>s so that they can be
 * authorized to certain actions. Example to roles are: Admin, Guest, User etc.
 * 
 * Please note that name of the role is unique.
 * 
 * @author Kaan Yamanyar
 * 
 */
@Entity
// name should be unique.
@Table(uniqueConstraints = @UniqueConstraint(columnNames = { "name" }))
public class Role {
	private Long id;

	private String name;

	private String description;

	@Column(length = 400)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * 
	 * @return The id of user. Id is auto-generated by db.
	 */
	@Id
	@GeneratedValue
	public Long getId() {
		return this.id;
	}

	@Column(length = 40)
	@NotNull
	/**
	 * Role's name. If loaded from db, it is not supoosed to be null.
	 * 
	 * @return Name of the role. Max 40 chars allowed.
	 */
	public String getName() {
		return this.name;
	}

	/**
	 * @param name
	 *            The name to set. Max 40 chars allowed.
	 */
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public boolean equals(Object obj) {
		if (this.name == null) {
			return false;
		}
		return this.name.equals(obj.toString());
	}

	@Override
	public int hashCode() {
		if (this.id == null) {
			return 0;
		}
		return (int) (this.id / 10) + 1;
	}

	@Override
	public String toString() {
		return this.getName();
	}

	/**
	 * This method is not supposed to be called by a developer.
	 * 
	 * @param id
	 */
	public void setId(Long id) {
		this.id = id;
	}

}